import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;

/**
 * ClassName: mazeFrame
 * Package: PACKAGE_NAME
 *
 * @Author: shangyuan_wu
 * @Create: 2022/12/15 - 14:01
 * Description:
 */
public class MazeFrame extends JFrame implements ActionListener {
    private final MazePanel mazepanel = new MazePanel();
    private static boolean startFlag = false;
    private static Timer timer;
    private static int seconds;
    private int i = 0;
    private int j = 0;

    public MazeFrame() {
        //设置标题
        this.setTitle("迷宫游戏_1.0");
        //设置窗体大小
        this.setSize(710, 768);
        //关闭后进程退出
        this.setDefaultCloseOperation(EXIT_ON_CLOSE);
        //设置居中
        this.setLocationRelativeTo(null);
        //

        //不允许调整窗口大小
        this.setResizable(false);
        //将JPanel组件添加到JFrame窗口
        this.add(mazepanel);
        //将JMenuBar添加到JFrame上
        this.createMenu();
        //加入键盘监听，完成米老鼠的移动
        this.createKeyListener();


        //窗体可显示
        this.setVisible(true);


    }


    private void createKeyListener() {
        //键盘监听需要实现KeyListener接口，选用适配器可以避免空实现，减少代码量
        KeyAdapter keyadapter = new KeyAdapter() {
            //按下
            @Override
            public void keyPressed(KeyEvent e) {
                int key = e.getKeyCode();
                if (startFlag) {
                    switch (key) {
                        //向上
                        case KeyEvent.VK_UP, KeyEvent.VK_W -> mazepanel.mouseMove(Constant.TOP);


                        //向右
                        case KeyEvent.VK_RIGHT, KeyEvent.VK_D -> mazepanel.mouseMove(Constant.RIGHT);


                        //向下
                        case KeyEvent.VK_DOWN, KeyEvent.VK_S -> mazepanel.mouseMove(Constant.BOTTOM);


                        //向左
                        case KeyEvent.VK_LEFT, KeyEvent.VK_A -> mazepanel.mouseMove(Constant.LEFT);
                        default -> {
                        }
                    }
                }

            }
        };
        //给主frame添加键盘监听
        this.addKeyListener(keyadapter);
    }

    private void createMenu() {
        //创建JMenuBar
        JMenuBar jmb = new JMenuBar();
        //创建游戏选项
        JMenu jMenu1 = new JMenu("游戏");
        //创建帮助选项
        JMenu jMenu2 = new JMenu("帮助");
        //创建闯关选项
        JMenu jMenu3 = new JMenu("闯关");
        //创建倒计时选项
        JMenu countdownMenu = new JMenu("0:00");
        //创建JMenu的多个子菜单
        JMenuItem jmi1 = new JMenuItem("新游戏");
        JMenuItem jmi2 = new JMenuItem("退出");
        JMenuItem jmi3 = new JMenuItem("操作帮助");
        JMenuItem jmi4 = new JMenuItem("胜利条件");
        JMenuItem jmi5 = new JMenuItem("简单");
        JMenuItem jmi6 = new JMenuItem("一般");
        JMenuItem jmi7 = new JMenuItem("困难");

        //添加一个开始按钮
        JButton startButton = new JButton("Start");
        startButton.setFocusable(false);
        JButton bfsButton = new JButton("BFS");
        bfsButton.setFocusable(false);


        //jmi1 jmi2添加到菜单项“游戏”中
        jMenu1.add(jmi1);
        jMenu1.add(jmi2);
        //jmi13 jmi4添加到菜单项“游戏”中
        jMenu2.add(jmi3);
        jMenu2.add(jmi4);
        //jmi5,jmi6,jmi7添加到菜单项"闯关"中
        jMenu3.add(jmi5);
        jMenu3.add(jmi6);
        jMenu3.add(jmi7);
        //将两个JMenu添加到JMenuBar上
        jmb.add(jMenu1);
        jmb.add(jMenu2);
        jmb.add(jMenu3);
        jmb.add(countdownMenu);
        jmb.add(startButton);
        jmb.add(bfsButton);
        //将JMenuBar添加到主Frame中
        this.setJMenuBar(jmb);

        timer = new Timer(1000, new ActionListener() {
            // 倒计时时间（单位：秒）
//            seconds = 60;

            @Override
            public void actionPerformed(ActionEvent e) {
                // 更新菜单项的文本
                countdownMenu.setText(String.format("0:%02d", seconds));
                // 倒计时减1
                if (seconds != 0) {
                    seconds--;
                } else {
                    // 倒计时结束，停止计时器
                    timer.stop();
                    // 弹出消息对话框
                    mazepanel.gameOver();
                }
            }
        });

        //添加监听
        jmi1.addActionListener(this);
        jmi2.addActionListener(this);
        jmi3.addActionListener(this);
        jmi4.addActionListener(this);
        jmi5.addActionListener(this);
        jmi6.addActionListener(this);
        jmi7.addActionListener(this);
        startButton.addActionListener(this);
        bfsButton.addActionListener(this);
        //设置指令
        jmi1.setActionCommand("restart");
        jmi2.setActionCommand("exit");
        jmi3.setActionCommand("help");
        jmi4.setActionCommand("win");
        jmi5.setActionCommand("simple");
        jmi6.setActionCommand("general");
        jmi7.setActionCommand("hard");
        startButton.setActionCommand("start");
        bfsButton.setActionCommand("bfs");

    }


    @Override
    public void actionPerformed(ActionEvent e) {
        String command = e.getActionCommand();
        if ("exit".equals(command)) {
            Object[] options = {"确定", "取消"};
            int response = JOptionPane.showOptionDialog(this, "您确认要退出吗", "警告！",
                    JOptionPane.YES_NO_OPTION, JOptionPane.QUESTION_MESSAGE, null,
                    options, options[0]);
            if (response == 0) {
                System.exit(0);
            }
        } else if ("restart".equals(command)) {
            mazepanel.restart();
        } else if ("help".equals(command)) {
            JOptionPane.showMessageDialog(null, "通过键盘的上下左右来移动",
                    "提示！", JOptionPane.INFORMATION_MESSAGE);
        } else if ("win".equals(command)) {
            JOptionPane.showMessageDialog(null, "在规定时间内移动到终点获得胜利",
                    "提示！", JOptionPane.INFORMATION_MESSAGE);
        } else if ("simple".equals(command)) {
            seconds = 90;
        } else if ("general".equals(command)) {
            seconds = 60;
        } else if ("hard".equals(command)) {
            seconds = 30;
        } else if ("start".equals(command)) {
            timer.start(); // 开始计时
            startFlag = true;
        } else if ("bfs".equals(command)){
            mazepanel.run();
        }

    }
}